<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/%E5%8C%BA%E5%9D%97%E9%93%BE200.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/%E5%8C%BA%E5%9D%97%E9%93%BE32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/%E5%8C%BA%E5%9D%97%E9%93%BE16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <link rel="manifest" href="/manifest.json">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css" integrity="sha256-2H3fkXt6FEmrReK448mDVGKb3WW2ZZw35gI7vqHOE4Y=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.css" integrity="sha256-no0c5ccDODBwp+9hSmV5VvPpKwHCpbVzXHexIkupM6U=" crossorigin="anonymous">
  <script src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.js" integrity="sha256-a5YRB27CcBwBFcT5EF/f3E4vzIqyHrSR878nseNYw64=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"zi-rui.gitee.io","root":"/","images":"/images","scheme":"Muse","version":"8.6.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":true,"color":"#222","save":"manual"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"swing","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>
<meta name="description" content="前几天无意间发觉自己在大学学了两个学期的计算机，编程却还没有离开过控制台console，这显然是不够的。一边想为了学以致用搞几个桌面应用，一边却又对C&#x2F;C++搞桌面应用望而却步（问就是水平不够QAQ）。于是只能拿听说简单到爆的C# Winform来练练手了。&#x2F;&#x2F;因为是简单上手,所以底层的、手动的东西不会碰，就玩玩图形界面和API  1.创建项目">
<meta property="og:type" content="article">
<meta property="og:title" content="C sharp Winform">
<meta property="og:url" content="https://zi-rui.gitee.io/2022/01/21/C-sharp-Winform/index.html">
<meta property="og:site_name" content="Zi-Rui&#39;s Blog">
<meta property="og:description" content="前几天无意间发觉自己在大学学了两个学期的计算机，编程却还没有离开过控制台console，这显然是不够的。一边想为了学以致用搞几个桌面应用，一边却又对C&#x2F;C++搞桌面应用望而却步（问就是水平不够QAQ）。于是只能拿听说简单到爆的C# Winform来练练手了。&#x2F;&#x2F;因为是简单上手,所以底层的、手动的东西不会碰，就玩玩图形界面和API  1.创建项目">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://pic.imgdb.cn/item/61eab4f72ab3f51d91e98a21.jpg">
<meta property="article:published_time" content="2022-01-21T14:57:26.000Z">
<meta property="article:modified_time" content="2022-01-21T15:03:30.705Z">
<meta property="article:author" content="Huang Zirui">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://pic.imgdb.cn/item/61eab4f72ab3f51d91e98a21.jpg">


<link rel="canonical" href="https://zi-rui.gitee.io/2022/01/21/C-sharp-Winform/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://zi-rui.gitee.io/2022/01/21/C-sharp-Winform/","path":"2022/01/21/C-sharp-Winform/","title":"C sharp Winform"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>C sharp Winform | Zi-Rui's Blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>

<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Zi-Rui's Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">3</span></a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">5</span></a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E5%88%9B%E5%BB%BA%E9%A1%B9%E7%9B%AE"><span class="nav-number">1.</span> <span class="nav-text">1.创建项目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E4%BB%A3%E7%A0%81%E6%96%87%E4%BB%B6%E5%88%86%E7%B1%BB%EF%BC%88%E4%B8%BB%E8%A6%81%E5%85%B3%E6%B3%A8%E7%9A%84%EF%BC%89%EF%BC%9A"><span class="nav-number">2.</span> <span class="nav-text">2.代码文件分类（主要关注的）：</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Form1-cs-%E6%BA%90%E7%A0%81%E6%96%87%E4%BB%B6%EF%BC%88%E7%AA%97%E5%8F%A3%EF%BC%89-%EF%BC%9A%E4%B8%9A%E5%8A%A1%E4%BB%A3%E7%A0%81"><span class="nav-number">2.1.</span> <span class="nav-text">Form1.cs                  源码文件（窗口）         ：业务代码</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Program-cs-%E6%BA%90%E7%A0%81%E6%96%87%E4%BB%B6%EF%BC%88Main%E5%87%BD%E6%95%B0%EF%BC%89"><span class="nav-number">2.2.</span> <span class="nav-text">Program.cs               源码文件（Main函数）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Form1-Designer-cs-%E6%BA%90%E7%A0%81%E6%96%87%E4%BB%B6%EF%BC%88%E7%95%8C%E9%9D%A2%E8%AE%BE%E8%AE%A1%EF%BC%89-%EF%BC%9A%E7%95%8C%E9%9D%A2%E4%BB%A3%E7%A0%81%EF%BC%8C%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%EF%BC%8C%E4%B8%80%E8%88%AC%E4%B8%8D%E6%89%8B%E5%8A%A8%E4%BF%AE%E6%94%B9"><span class="nav-number">2.3.</span> <span class="nav-text">Form1.Designer.cs   源码文件（界面设计）  ：界面代码，自动生成，一般不手动修改</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E6%8E%A7%E4%BB%B6%E7%9A%84%E6%B7%BB%E5%8A%A0"><span class="nav-number">3.</span> <span class="nav-text">3.控件的添加</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%82%BB%E7%93%9C%E6%B7%BB%E5%8A%A0%EF%BC%8C%E5%8F%B3%E9%94%AE-gt-%E5%B1%9E%E6%80%A7-gt-%EF%BC%88%E6%9D%A5%E4%BF%AE%E6%94%B9%E6%8E%A7%E4%BB%B6%E5%B1%9E%E6%80%A7%EF%BC%89"><span class="nav-number">3.1.</span> <span class="nav-text">傻瓜添加，右键-&gt;属性-&gt;（来修改控件属性）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%89%8B%E5%8A%A8%E6%B7%BB%E5%8A%A0%EF%BC%8C%E7%95%A5"><span class="nav-number">3.2.</span> <span class="nav-text">手动添加，略</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-%E4%BA%8B%E4%BB%B6%E7%9A%84%E6%B7%BB%E5%8A%A0"><span class="nav-number">4.</span> <span class="nav-text">4.事件的添加</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-%E5%82%BB%E7%93%9C%E6%8E%A7%E4%BB%B6%E5%A4%84%E7%90%86"><span class="nav-number">4.1.</span> <span class="nav-text">1.傻瓜控件处理</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E2%91%A0%E5%8F%B3%E9%94%AE-gt-%E5%B1%9E%E6%80%A7-gt-%E5%B0%8F%E9%97%AA%E7%94%B5%EF%BC%88%E5%8D%B3%E4%BA%8B%E4%BB%B6%EF%BC%89"><span class="nav-number">4.1.1.</span> <span class="nav-text">①右键-&gt;属性-&gt;小闪电（即事件）</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E2%91%A1%E5%9C%A8%E5%AF%B9%E6%8E%A7%E4%BB%B6%E7%9A%84%E5%AF%B9%E5%BA%94%E6%93%8D%E4%BD%9C%E4%B8%AD%EF%BC%8C%E5%86%99%E5%85%A5%E9%9C%80%E8%A6%81%E7%9A%84%E6%96%B9%E6%B3%95%E5%90%8D%EF%BC%8C%E5%8D%B3%E4%BC%9A%E5%9C%A8Form1-cs%E4%B8%AD%E8%87%AA%E5%8A%A8%E5%88%9B%E5%BB%BA%E5%AF%B9%E5%BA%94%E7%9A%84%E7%A9%BA%E6%96%B9%E6%B3%95%EF%BC%88%E5%87%BD%E6%95%B0%EF%BC%89"><span class="nav-number">4.1.2.</span> <span class="nav-text">②在对控件的对应操作中，写入需要的方法名，即会在Form1.cs中自动创建对应的空方法（函数）</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E2%91%A2%E7%BC%96%E8%BE%91%E8%AF%A5%E6%96%B9%E6%B3%95"><span class="nav-number">4.1.3.</span> <span class="nav-text">③编辑该方法</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-%E6%89%8B%E5%8A%A8%E6%8E%A7%E4%BB%B6%E5%A4%84%E7%90%86%EF%BC%8C%E7%95%A5"><span class="nav-number">4.2.</span> <span class="nav-text">2.手动控件处理，略</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-%E6%8E%A7%E4%BB%B6%E7%9A%84%E5%B8%83%E5%B1%80"><span class="nav-number">5.</span> <span class="nav-text">5.控件的布局</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A0%E5%B8%83%E5%B1%80%E6%96%B9%E6%B3%95"><span class="nav-number">5.1.</span> <span class="nav-text">①布局方法</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E5%82%BB%E7%93%9C%E5%BC%8F%E5%B8%83%E5%B1%80%EF%BC%9A%E7%9B%B4%E6%8E%A5%E5%9C%A8%E5%9B%BE%E5%BD%A2%E7%95%8C%E9%9D%A2%E4%BF%AE%E6%94%B9%E5%B8%83%E5%B1%80"><span class="nav-number">5.1.1.</span> <span class="nav-text">傻瓜式布局：直接在图形界面修改布局</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%89%8B%E5%8A%A8%E5%B8%83%E5%B1%80%EF%BC%8C%E7%95%A5"><span class="nav-number">5.1.2.</span> <span class="nav-text">手动布局，略</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E5%B8%83%E5%B1%80%E5%99%A8%E5%B8%83%E5%B1%80%EF%BC%8C%E7%95%A5%EF%BC%88%E9%BB%98%E8%AE%A4%E6%AF%8F%E4%B8%AAform%E5%92%8Cpanel%E9%83%BD%E6%9C%89%E4%B8%80%E4%B8%AA%EF%BC%8C%E8%A6%81%E7%94%A8%E5%86%8D%E6%9F%A5%EF%BC%89"><span class="nav-number">5.1.3.</span> <span class="nav-text">布局器布局，略（默认每个form和panel都有一个，要用再查）</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A1%E5%85%B3%E4%BA%8E%E6%8E%A7%E4%BB%B6%E5%B1%9E%E6%80%A7"><span class="nav-number">5.2.</span> <span class="nav-text">②关于控件属性</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Anchor%EF%BC%9A%E9%94%9A%E5%AE%9A%EF%BC%8C%E5%9B%BA%E5%AE%9A%E6%8E%A7%E4%BB%B6%E4%B8%8E%E7%AA%97%E5%8F%A3%E6%9F%90%E8%BE%B9%E7%9A%84%E8%BE%B9%E8%B7%9D"><span class="nav-number">5.3.</span> <span class="nav-text">Anchor：锚定，固定控件与窗口某边的边距</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Dock%EF%BC%9A%E5%81%9C%E9%9D%A0%EF%BC%8C%E5%B0%86%E6%8E%A7%E4%BB%B6%E5%9B%BA%E5%AE%9A%E5%9C%A8%E7%AA%97%E5%8F%A3%E7%9A%84%E4%B8%AD%E5%A4%AE%E6%88%96%E4%B8%80%E4%BE%A7%EF%BC%88%E4%B8%80%E8%88%AC%E7%94%A8%E4%BA%8EPanel%E3%80%90%E9%9D%A2%E6%9D%BF%E3%80%91%E6%88%96PictureBox%E7%AD%89%E6%8E%A7%E4%BB%B6%EF%BC%8C%E5%BD%A2%E6%88%90%E5%88%86%E5%89%B2%E5%B8%83%E5%B1%80%EF%BC%89"><span class="nav-number">5.4.</span> <span class="nav-text">Dock：停靠，将控件固定在窗口的中央或一侧（一般用于Panel【面板】或PictureBox等控件，形成分割布局）</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-UserControl%E6%8E%A7%E4%BB%B6"><span class="nav-number">6.</span> <span class="nav-text">6.UserControl控件</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E5%A4%8D%E5%90%88%EF%BC%88%E7%94%A8%E6%88%B7%EF%BC%89%E7%9A%84%E4%BD%BF%E7%94%A8%EF%BC%9A"><span class="nav-number">6.1.</span> <span class="nav-text">一、复合（用户）的使用：</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E2%91%A0%E7%94%A8%E6%88%B7%E6%8E%A7%E4%BB%B6%E6%B7%BB%E5%8A%A0-%E7%B2%98%E8%B4%B4%E8%BF%9B%E9%A1%B9%E7%9B%AE%E5%90%8E%EF%BC%8C%E9%9C%80%E8%A6%81%E9%87%8D%E6%96%B0%E7%94%9F%E6%88%90%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%E5%90%8E%EF%BC%8C%E6%89%8D%E8%83%BD%E5%9C%A8%E5%B7%A5%E5%85%B7%E7%AE%B1%E6%89%BE%E5%88%B0"><span class="nav-number">6.1.1.</span> <span class="nav-text">①用户控件添加&#x2F;粘贴进项目后，需要重新生成解决方案后，才能在工具箱找到</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E2%91%A1%E5%AD%90%E6%8E%A7%E4%BB%B6%E9%9C%80%E8%A6%81%E6%94%B9%E4%B8%BApublic%E5%90%8E%EF%BC%8Cform%E6%89%8D%E8%83%BD%E6%AD%A3%E5%B8%B8%E8%AE%BF%E9%97%AE"><span class="nav-number">6.1.2.</span> <span class="nav-text">②子控件需要改为public后，form才能正常访问</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%A4%8D%E5%90%88%EF%BC%88%E7%94%A8%E6%88%B7%EF%BC%89%E7%9A%84%E6%B7%BB%E5%8A%A0%EF%BC%9A%E4%B8%BB%E7%AA%97%E4%BD%93%E6%96%87%E4%BB%B6%E5%8F%B3%E9%94%AE%E6%B7%BB%E5%8A%A0%E7%94%A8%E6%88%B7%E6%8E%A7%E4%BB%B6%EF%BC%88%E6%AF%8F%E6%AC%A1%E4%BF%AE%E6%94%B9%E8%AF%A5%E6%8E%A7%E4%BB%B6%E9%9C%80%E8%A6%81%E5%88%B7%E6%96%B0%EF%BC%89"><span class="nav-number">6.2.</span> <span class="nav-text">二、复合（用户）的添加：主窗体文件右键添加用户控件（每次修改该控件需要刷新）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E8%87%AA%E5%AE%9A%E4%B9%89%E5%B1%9E%E6%80%A7%EF%BC%8C%E7%95%A5"><span class="nav-number">6.3.</span> <span class="nav-text">三、自定义属性，略</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E8%87%AA%E5%AE%9A%E4%B9%89%E4%BA%8B%E4%BB%B6%EF%BC%8C%E7%95%A5"><span class="nav-number">6.4.</span> <span class="nav-text">四、自定义事件，略</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-%E5%90%8E%E7%BB%AD%EF%BC%8C%E7%95%A5"><span class="nav-number">7.</span> <span class="nav-text">7.后续，略</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%90%8E%E9%9D%A2%E7%9A%84%E5%86%85%E5%AE%B9%E6%9A%82%E6%97%B6%E6%B2%A1%E6%9C%89%E5%AD%A6%E4%B9%A0%E9%9C%80%E6%B1%82"><span class="nav-number">7.1.</span> <span class="nav-text">后面的内容暂时没有学习需求</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-overview">
            <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Huang Zirui"
      src="https://pic.imgdb.cn/item/611b33f24907e2d39cecfabb.gif">
  <p class="site-author-name" itemprop="name">Huang Zirui</p>
  <div class="site-description" itemprop="description">拿来主义</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">5</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">3</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="mailto:1745605250@qq.com" title="E-Mail → mailto:1745605250@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



          </div>
        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://zi-rui.gitee.io/2022/01/21/C-sharp-Winform/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://pic.imgdb.cn/item/611b33f24907e2d39cecfabb.gif">
      <meta itemprop="name" content="Huang Zirui">
      <meta itemprop="description" content="拿来主义">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Zi-Rui's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          C sharp Winform<a href="https://gitee.com/Zi-Rui/Zi-Rui/tree/master/source/_posts/_posts/C-sharp-Winform.md" class="post-edit-link" title="编辑" rel="noopener" target="_blank"><i class="fa fa-pen-nib"></i></a>
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2022-01-21 22:57:26 / 修改时间：23:03:30" itemprop="dateCreated datePublished" datetime="2022-01-21T22:57:26+08:00">2022-01-21</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Winform/" itemprop="url" rel="index"><span itemprop="name">Winform</span></a>
        </span>
    </span>

  
    <span id="/2022/01/21/C-sharp-Winform/" class="post-meta-item leancloud_visitors" data-flag-title="C sharp Winform" title="阅读次数">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span class="leancloud-visitors-count"></span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <blockquote>
<p>前几天无意间发觉自己在大学学了两个学期的计算机，编程却还没有离开过控制台console，这显然是不够的。一边想为了学以致用搞几个桌面应用，一边却又对C/C++搞桌面应用望而却步（问就是水平不够QAQ）。于是只能拿听说简单到爆的C# Winform来练练手了。//因为是简单上手,所以底层的、手动的东西不会碰，就玩玩图形界面和API</p>
</blockquote>
<h3 id="1-创建项目"><a href="#1-创建项目" class="headerlink" title="1.创建项目"></a>1.创建项目</h3><span id="more"></span>

<img src="https://pic.imgdb.cn/item/61eab4f72ab3f51d91e98a21.jpg" style="zoom: 50%;">

<h3 id="2-代码文件分类（主要关注的）："><a href="#2-代码文件分类（主要关注的）：" class="headerlink" title="2.代码文件分类（主要关注的）："></a>2.代码文件分类（主要关注的）：</h3><ol>
<li><h4 id="Form1-cs-源码文件（窗口）-：业务代码"><a href="#Form1-cs-源码文件（窗口）-：业务代码" class="headerlink" title="Form1.cs                  源码文件（窗口）         ：业务代码"></a><em>Form1.cs</em>                  源码文件（窗口）         ：业务代码</h4></li>
<li><h4 id="Program-cs-源码文件（Main函数）"><a href="#Program-cs-源码文件（Main函数）" class="headerlink" title="Program.cs               源码文件（Main函数）"></a><em>Program.cs</em>               源码文件（Main函数）</h4></li>
<li><h4 id="Form1-Designer-cs-源码文件（界面设计）-：界面代码，自动生成，一般不手动修改"><a href="#Form1-Designer-cs-源码文件（界面设计）-：界面代码，自动生成，一般不手动修改" class="headerlink" title="Form1.Designer.cs   源码文件（界面设计）  ：界面代码，自动生成，一般不手动修改"></a><em>Form1.Designer.cs</em>   源码文件（界面设计）  ：界面代码，自动生成，一般不手动修改</h4></li>
</ol>
<h3 id="3-控件的添加"><a href="#3-控件的添加" class="headerlink" title="3.控件的添加"></a>3.控件的添加</h3><ol>
<li><h4 id="傻瓜添加，右键-gt-属性-gt-（来修改控件属性）"><a href="#傻瓜添加，右键-gt-属性-gt-（来修改控件属性）" class="headerlink" title="傻瓜添加，右键->属性->（来修改控件属性）"></a>傻瓜添加，右键-&gt;属性-&gt;（来修改控件属性）</h4></li>
<li><h4 id="手动添加，略"><a href="#手动添加，略" class="headerlink" title="手动添加，略"></a>手动添加，略</h4></li>
</ol>
<h3 id="4-事件的添加"><a href="#4-事件的添加" class="headerlink" title="4.事件的添加"></a>4.事件的添加</h3><h4 id="1-傻瓜控件处理"><a href="#1-傻瓜控件处理" class="headerlink" title="1.傻瓜控件处理"></a>1.傻瓜控件处理</h4><h5 id="①右键-gt-属性-gt-小闪电（即事件）"><a href="#①右键-gt-属性-gt-小闪电（即事件）" class="headerlink" title="①右键->属性->小闪电（即事件）"></a>①右键-&gt;属性-&gt;小闪电（即事件）</h5><h5 id="②在对控件的对应操作中，写入需要的方法名，即会在Form1-cs中自动创建对应的空方法（函数）"><a href="#②在对控件的对应操作中，写入需要的方法名，即会在Form1-cs中自动创建对应的空方法（函数）" class="headerlink" title="②在对控件的对应操作中，写入需要的方法名，即会在Form1.cs中自动创建对应的空方法（函数）"></a>②<em>在对控件的对应操作中，写入需要的方法名，即会在Form1.cs中自动创建对应的空方法（函数）</em></h5><h5 id="③编辑该方法"><a href="#③编辑该方法" class="headerlink" title="③编辑该方法"></a><strong>③编辑该方法</strong></h5><h4 id="2-手动控件处理，略"><a href="#2-手动控件处理，略" class="headerlink" title="2.手动控件处理，略"></a>2.手动控件处理，略</h4><figure class="highlight plaintext"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">//Postscript:</span><br><span class="line">1.事件处理回调定义在Form1.cs中</span><br><span class="line">2.可使用MessageBox.Show()弹出一个消息框</span><br><span class="line">3.在GUI程序中，看不到控制台输出</span><br></pre></td></tr></tbody></table></figure>



<h3 id="5-控件的布局"><a href="#5-控件的布局" class="headerlink" title="5.控件的布局"></a>5.控件的布局</h3><h4 id="①布局方法"><a href="#①布局方法" class="headerlink" title="①布局方法"></a>①布局方法</h4><ul>
<li><h5 id="傻瓜式布局：直接在图形界面修改布局"><a href="#傻瓜式布局：直接在图形界面修改布局" class="headerlink" title="傻瓜式布局：直接在图形界面修改布局"></a>傻瓜式布局：直接在图形界面修改布局</h5><figure class="highlight plaintext"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">//postscript：</span><br><span class="line">按ctrl选择多个目标可以选择对齐样式，但不随窗体的缩放而改变（即用户缩放了窗口，但控件位置不变）</span><br></pre></td></tr></tbody></table></figure></li>
<li><h5 id="手动布局，略"><a href="#手动布局，略" class="headerlink" title="手动布局，略"></a>手动布局，略</h5></li>
<li><h5 id="布局器布局，略（默认每个form和panel都有一个，要用再查）"><a href="#布局器布局，略（默认每个form和panel都有一个，要用再查）" class="headerlink" title="布局器布局，略（默认每个form和panel都有一个，要用再查）"></a>布局器布局，略<em>（默认每个form和panel都有一个，要用再查）</em></h5></li>
</ul>
<h4 id="②关于控件属性"><a href="#②关于控件属性" class="headerlink" title="②关于控件属性"></a>②关于控件属性</h4><ul>
<li><h4 id="Anchor：锚定，固定控件与窗口某边的边距"><a href="#Anchor：锚定，固定控件与窗口某边的边距" class="headerlink" title="Anchor：锚定，固定控件与窗口某边的边距"></a>Anchor：锚定，固定控件与窗口某边的边距</h4></li>
<li><h4 id="Dock：停靠，将控件固定在窗口的中央或一侧（一般用于Panel【面板】或PictureBox等控件，形成分割布局）"><a href="#Dock：停靠，将控件固定在窗口的中央或一侧（一般用于Panel【面板】或PictureBox等控件，形成分割布局）" class="headerlink" title="Dock：停靠，将控件固定在窗口的中央或一侧（一般用于Panel【面板】或PictureBox等控件，形成分割布局）"></a>Dock：停靠，将控件固定在窗口的中央或一侧（一般用于Panel【面板】或PictureBox等控件，形成分割布局）</h4></li>
</ul>
<h3 id="6-UserControl控件"><a href="#6-UserControl控件" class="headerlink" title="6.UserControl控件"></a>6.UserControl控件</h3><p><em>即官方控件的组合</em></p>
<h4 id="一、复合（用户）的使用："><a href="#一、复合（用户）的使用：" class="headerlink" title="一、复合（用户）的使用："></a><strong>一、</strong>复合（用户）的<strong>使用：</strong></h4><h5 id="①用户控件添加-粘贴进项目后，需要重新生成解决方案后，才能在工具箱找到"><a href="#①用户控件添加-粘贴进项目后，需要重新生成解决方案后，才能在工具箱找到" class="headerlink" title="①用户控件添加/粘贴进项目后，需要重新生成解决方案后，才能在工具箱找到"></a>①用户控件添加/粘贴进项目后，<strong>需要重新生成解决方案后，才能在工具箱找到</strong></h5><h5 id="②子控件需要改为public后，form才能正常访问"><a href="#②子控件需要改为public后，form才能正常访问" class="headerlink" title="②子控件需要改为public后，form才能正常访问"></a>②<strong>子控件需要改为</strong>public后，form才能正常访问</h5><h4 id="二、复合（用户）的添加：主窗体文件右键添加用户控件（每次修改该控件需要刷新）"><a href="#二、复合（用户）的添加：主窗体文件右键添加用户控件（每次修改该控件需要刷新）" class="headerlink" title="二、复合（用户）的添加：主窗体文件右键添加用户控件（每次修改该控件需要刷新）"></a>二、<strong>复合（用户）的</strong>添加：主窗体文件右键添加用户控件（每次修改该控件需要刷新）</h4><h4 id="三、自定义属性，略"><a href="#三、自定义属性，略" class="headerlink" title="三、自定义属性，略"></a>三、自定义属性，略</h4><h4 id="四、自定义事件，略"><a href="#四、自定义事件，略" class="headerlink" title="四、自定义事件，略"></a>四、自定义事件，略</h4><h3 id="7-后续，略"><a href="#7-后续，略" class="headerlink" title="7.后续，略"></a>7.后续，略</h3><h4 id="后面的内容暂时没有学习需求"><a href="#后面的内容暂时没有学习需求" class="headerlink" title="后面的内容暂时没有学习需求"></a>后面的内容暂时没有学习需求</h4><p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1d7411F7PG?p=32">C# WinForm入门课程_图形界面GUI编程_62集视频教程_哔哩哔哩_bilibili</a></p>

    </div>

    
    
    

    <footer class="post-footer">

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/01/21/Hexo-Blog%E6%93%8D%E4%BD%9C%E5%A4%87%E5%BF%98/" rel="prev" title="Hexo Blog操作备忘">
                  <i class="fa fa-chevron-left"></i> Hexo Blog操作备忘
                </a>
            </div>
            <div class="post-nav-item">
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Huang Zirui</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script size="300" alpha="0.6" zIndex="-1" src="https://cdn.jsdelivr.net/npm/ribbon.js@1.0.2/dist/ribbon.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script>

  
<script src="/js/third-party/search/local-search.js"></script>



  <script class="next-config" data-name="nprogress" type="application/json">{"enable":true,"spinner":true}</script>
  <script src="/js/third-party/nprogress.js"></script>

  


  <script class="next-config" data-name="leancloud_visitors" type="application/json">{"enable":true,"app_id":"Ovh9tn2aUaXAFqf3FNK3OXfa-9Nh9j0Va# \u003cyour app id>","app_key":"BF04bydyF2jpJ8WemKIpzDoT# \u003cyour app key>","server_url":null,"security":true}</script>
  <script src="/js/third-party/statistics/lean-analytics.js"></script>



</body>
</html>
